/*
 *  Copyright 2010 UPF Telecos 4th group G.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.listeners;

import edu.upf.network.impl.XMPPClient;
import edu.upf.utils.LogSystem;
import java.util.Collection;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;

/**
 * Implements the listener that handles the changes in the user roster (list of
 * contacts)
 * @author netjois
 */
public class ContactListener implements RosterListener {

    private XMPPClient jabberClient;
    private LogSystem log;

    /**
     * Constructor of the listener, receives the client logic object for
     * callbacks
     * @param jabberClient
     */
    public ContactListener(XMPPClient jabberClient) {
        this.jabberClient=jabberClient;
        log = new LogSystem(ContactListener.class);
    }

    /**
     * Defines the actions to follow when a user presence is changed
     * @param presence 
     */
    @Override
    public void presenceChanged(Presence presence) {
        // Si se imprime null es que la presencia no está disponible
        jabberClient.updatePresence(presence);
        //log.addDebugLog(presence.getFrom()+"presenceChanged: "+presence.getStatus());
    }

    /**
     * Defines the actions to follow when a user presence is changed
     * @param arg0
     */
    @Override
    public void entriesAdded(Collection<String> arg0) {
    }

    /**
     * Defines the actions to follow when a roster entry is deleted
     * @param arg0
     */
    @Override
    public void entriesDeleted(Collection<String> arg0) {
    }

    /**
     * Defines the actions to follow when roster entry is updated
     * @param arg0
     */
    @Override
    public void entriesUpdated(Collection<String> arg0) {
        log.addDebugLog("Updating entries");
        jabberClient.updateEntries(arg0);
    }
}
